﻿using Core.DataAccess.Model.Project.Order;
using Core.Framework.Model.Common;
using Core.Framework.Util;
using Core.IBusiness.IOrderModule;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;

namespace Core.Web.Controllers.Order
{
    /// <summary>
    /// 用户购物车管理
    /// </summary>
    [Route(".v1/Order/[controller]")]
    [ApiController]
    [ApiVerification(IBusiness.IProjectModule.Model.APIServiceEnum.Shop)]
    public class OrderUserCartController : BaseApiController
    {
        public OrderUserCartController(IOrder iOrder) : 
            base(iOrder: iOrder)
        {
        
        }

        /// <summary>
        /// 获取购物车信息
        /// </summary>
        /// <returns></returns>
        [HttpPost(".getCart")]
        public IResult GetCart(Pagination pagination)
        {
            return this.ExecMethod(
               () => {
                   return this.Result(this.iOrder
                       .GetUserCart(
                       iClientInfo.Key, 
                       iProjectInfo.Token, 
                       pagination), pagination);
               });
        }




        /// <summary>
        /// 加入用户购物车
        /// </summary>
        /// <param name="model">添加参数</param>
        /// <returns>返回购物ID</returns>
        [HttpPost(".reg")]
        public IResult reg(OrderUserCart model)
        {
           return this.ExecMethod(
               () => {
                   return this.Result(this.iOrder.AddUserCart(model));
               }, model.IsValid(IsValidEnum.reg));
        }



        /// <summary>
        /// 根据购物车下单
        /// </summary>
        /// <param name="request">订单请求信息</param>
        /// <returns></returns>
        [HttpPost(".regOrder")]
        public IResult UserCartOrder(OrderRequest request)
        {
            request.Page(iClientInfo,iProjectInfo);

            return this.ExecMethod(
               () => { 

                   return this.Result(this.iOrder.UserCartOrder(
                       request.orderBasis,
                       request.address,
                       request.userCarts));
               }
               , request.orderBasis.IsValid(IsValidEnum.reg)
               , request.userCarts.IsValid(IsValidEnum.reg)
               , request.address.IsValid(IsValidEnum.reg));
        }



        /// <summary>
        /// 修改购物车数量信息
        /// </summary>
        /// <param name="model">修改参数模型</param>
        /// <returns></returns>
        [HttpPut]
        public IResult Update(OrderUserCart model)
        {
            return this.ExecMethod(
               () => {
                   return this.Result(this.iOrder.UpdateUserCartItem(model));
               }, model.IsValid(IsValidEnum.update));
        }


        


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key">购物关键字</param>
        /// <returns>返回值受影响行数</returns>
        [HttpDelete("{key}")]
        public IResult delete(int key)
        {
            return this.ExecMethod(
               () => {
                   return this.Result(this.iOrder
                       .DeleteUserCart(key, this.iProjectInfo.Token));
               }, 
               new Tuple<bool, string>(key > 0, "params: key is null")
               );
        }


        

    }
}